package com.heimali.sf.utils;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.os.IBinder;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.heimali.sf.SiFaApplication;

public class AndroidUtils {

	private static PackageManager pm = null;

	public static void toast(Context context, int message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	public static void displayToast(Context context, String message) {
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	/**
	 * ProgressDialog(message类型为int)
	 */
	public static ProgressDialog buildCancelDialog(Context context, int message, int style) {
		ProgressDialog dialog = new ProgressDialog(context);
		dialog.setMessage(context.getString(message));
		int STYLE = style;
		if (STYLE != -1) {
			dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			dialog.setProgress(100);
		}
		dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_SEARCH) {
					return true;
				}
				return false;
			}
		});// 不可被搜索键取消对话框
		dialog.setCancelable(false);// 不可被返回键取消对话框
		return dialog;
	}

	public static ProgressDialog buildDialog(Context context, int message, int style) {
		ProgressDialog dialog = new ProgressDialog(context);
		dialog.setMessage(context.getString(message));
		int STYLE = style;
		if (STYLE != -1) {
			dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			dialog.setProgress(100);
		}
		dialog.setCancelable(false);
		return dialog;
	}

	public static AlertDialog buildDialog(Context context, int title, int message, Integer positive,
			OnClickListener positiveOnClick, Integer negative, OnClickListener negativeOnClick) {

		return createBuilder(context, title, positive, positiveOnClick, negative, negativeOnClick).setMessage(message)
				.create();
	}

	public static AlertDialog buildDialog(Context context, int title, String message, Integer positive,
			OnClickListener positiveOnClick, Integer negative, OnClickListener negativeOnClick) {

		return createBuilder(context, title, positive, positiveOnClick, negative, negativeOnClick).setMessage(message)
				.create();
	}

	private static Builder createBuilder(Context context, int title, Integer positive, OnClickListener positiveOnClick,
			Integer negative, OnClickListener negativeOnClick) {

		AlertDialog.Builder builder = new Builder(context);
		builder.setTitle(title);
		builder.setPositiveButton(positive, positiveOnClick);
		if (negative != null && negativeOnClick != null)
			builder.setNegativeButton(negative, negativeOnClick);
		return builder;
	}

	public static AlertDialog buildDialog(Context context, int title, String[] items, OnClickListener onClick) {

		return new Builder(context).setTitle(title).setItems(items, onClick).setNegativeButton(null, null).create();
	}

	/**
	 * 弹出软键盘
	 * 
	 * @param context
	 */
	public static void displaySoftInput(final Context context) {
		// 键盘弹出需要延迟一会才能弹出
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}, 100);
	}

	/**
	 * 多种隐藏软件盘方法的其中一种
	 * 
	 * @param token
	 */
	public static void hideSoftInput(IBinder token) {
		if (token != null) {
			InputMethodManager imm = (InputMethodManager) SiFaApplication.getContext().getSystemService(
					Context.INPUT_METHOD_SERVICE);
			if (imm.isActive()) {
				imm.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
			}
		}
	}

	public static String getSDCardPath() {
		return Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
	}

	/**
	 * 版本号
	 */
	private static int VersionCode = 0;
	/**
	 * 版本名称
	 */
	private static String VersionName = "";

	/**
	 * 获取版本号
	 * 
	 * @return
	 */
	public static int getVersionCode() {
		if (VersionCode <= 0) {
			try {
				VersionCode = AndroidUtils.getPackageInfo(null).versionCode;
			} catch (Exception e) {
				VersionCode = 0;
			}
		}
		return VersionCode;
	}

	/**
	 * 获取版本名称
	 * 
	 * @return
	 */
	public static String getVersionName() {
		if (TextUtils.isEmpty(VersionName)) {
			try {
				VersionName = AndroidUtils.getPackageInfo(null).versionName;
			} catch (Exception e) {
				VersionName = "";
			}
		}
		return VersionName;
	}

	public static PackageInfo getPackageInfo(String packageName) {
		try {
			if (packageName == null)
				packageName = SiFaApplication.getContext().getPackageName();
			return getPackageManager().getPackageInfo(packageName, 0);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static PackageManager getPackageManager() {
		if (pm == null)
			pm = SiFaApplication.getContext().getPackageManager();
		return pm;
	}

}
